package com.manage.system.interceptor;

import com.manage.system.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.protocol.HttpContext;
import org.slf4j.MDC;

import java.io.IOException;

/**
 * @Author liuzhao
 * @Date 2023/2/23 22:42
 * @Content 实现httpclient拦截器
 * @Version 1.0
 */
@Slf4j
public class HttpClientTraceIdInterceptor implements HttpRequestInterceptor {
    @Override
    public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
        String traceId = MDC.get(Constants.TRACE_ID);
        log.info("这里有没有日志");
        // 当前线程中有traceId，则将traceId进行透传
        if (traceId != null) {
            // 添加请求体
            httpRequest.addHeader(Constants.TRACE_ID, traceId);
        }
    }
}
